<!--参数修改-合约-->
<template>
  <div class="bg bg1">
    <scroller class="scroller">
      <div class="wrapper" v-if="pairMap">
        
        <!--信号-->
        <VSignal class="senior" ref="signal" v-model="signal" />

        <!--参数设置-->
        <VParam class="senior" ref="VParam" v-model="senior" :isModify="true" :reserves="reserves" :pairMap="pairMap" :investCoin="pairMap.investCoin" :isRestore="false" @paramChange="paramChange"></VParam>
        
        <!--仓位风控-->
        <VCRisk class="control" ref="risk" v-model="isShow" :openThreshold="risk"></VCRisk>
        
        <!--其他设置-->
        <VCSet class="set" ref="upSet" v-model="other" :stopProfit="stopProfit" :stopLoss="stopLoss" :timeStopStamp ="timeStopStamp"></VCSet>
        <LSwitc class="follow" ref="LSwitc" v-if="other" v-model="isOpenFollow" :title="T('开放策略跟单')" @change="followChange"></LSwitc>
        <div class="height80"></div>
      </div>
    </scroller>

    <!--启动-->
    <div class="ok bg3">
      <div class="ok-but main1" @click="okClick">
        <text class="font32 text1">{{T('修改参数')}}</text>
      </div>
    </div>
  </div>
</template>

<script>

//组件
import VSignal from "@/up/view/VSignal.vue";
import VParam from "@/up/view/VParam.vue";
import VCRisk from "@/upContract/view/VCRisk.vue";
import VCSet from "@/upContract/view/VCSet.vue";
import LSwitc from "@/public/view/LSwitc.vue";

//功能
import navigator from "@/_js/navigator"; 
import modal from "@/_js/modal";
import math from "@/_js/math";
import broadcast from '@/_js/broadcast';

import weexHttp from "@/_js/https"; ;
const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('参数'));

export default {

  components: {
    VSignal,
    VParam,
    VCRisk,
    VCSet,
    LSwitc,
  },

  data() {
    return {
      T:T,
      math:math,
      executeId:'',
      strategyName:'', //策略名称
      configMap:undefined, //执行配置
      
      isOpenFollow:false, //turn 开放跟单
      followMimic:false, //turn 模拟盘开放跟单

      /************************************ 信号设置*/
      signal:undefined, //信号

      /************************************ 参数设置*/
      reserves:undefined, //参数数组
      pairMap:undefined, //交易对

      /************************************ 切换选中*/
      isShow: true,
      risk: undefined, //仓位风控
      stopProfit:undefined, //止盈
      stopLoss:undefined, //止损
      timeStopStamp:undefined, //终止时间戳

      
      /************************************ 切换选中*/
      senior: true, //true 显示参数设置
      other: true, //true 显示其他设置
    };
  },

  mounted() {

    this.executeId = navigator.getParam('executeId');
    this.strategyName = navigator.getParam('strategyName');
    this.configMap = navigator.getParam('configMap');

    this.followMap = navigator.getParam('followMap');
    this.isOpenFollow = navigator.getParam('isOpenFollow');
    this.followMimic = navigator.getParam('followMimic');

    /************************************ 信号设置设置*/
    var signalStates = navigator.getParam('signalStates');
    var signal = this.configMap.signal;
    if (signal) {
     signal.openSignalEdit = signalStates==0;
    }
    this.signal = signal;

    /************************************ 参数设置*/
    this.reserves = this.initData(this.configMap.reserves);
    this.pairMap = this.configMap.tradePairs[0];
    this.risk = math.cheng(this.pairMap.openThreshold,100,2, 2);
    //其他设置
    this.stopProfit = this.configMap.stopProfit.length ? math.cheng(this.configMap.stopProfit, '100') : '';
    this.stopLoss = this.configMap.stopLoss.length ? math.cheng(this.configMap.stopLoss, '-100') : '';
    this.timeStopStamp = this.configMap.stopTime;

  },

  methods: {
    
    //2021.8.5  处理老策略缺少网格分布开关参数显示异常问题。
    initData(reserves){

      for (let index = 0; index < reserves.length; index++) {
        var param = reserves[index];
        if (param.name == '网格分布') {
          break;
        }

        if (param.name == '网格间隔(等比)') {
          var value = Number(param.value) > 0 ? 0 : 1;
          var map = {'name':'网格分布', 'value':value};
          reserves.splice(index, 0, map);
          break;
        }
      }
      return reserves;
    },

    /********************************** http*/
    //http修改参数
    httpUpdateParam() {
      modal.showLoad();
      var param = {executeConfig : this.configMap,
                  executeId: this.executeId};
      weexHttp.postX("contract/editExecuteConfig",param,true,(data) => {
          modal.hideLoad();
          if (data.status == 200) {
            modal.toast(T("修改参数成功"))
            navigator.back();
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },
    
    //关闭跟单
    httCploseFollowStrategy(){

      modal.showLoad();
      var param = {executeId:this.executeId};
      weexHttp.postX("follow/closeFollowStrategy",param,true,(data)=>{
      modal.toast('dsfadsaf');
        modal.hideLoad();
        if(data.status == 200){
          modal.toast(T('关闭成功'));
          broadcast.send(broadcast.EXECUTE_UPDATE);
        } else {
          this.isOpenFollow = true;
          modal.toast(data.msg);
        }
      });
    },

    /********************************** 功能*/
    //开启跟单
    openFollow(){
      setTimeout(() => {
        this.isOpenFollow = false;
      }, 100);

      var parm = {executeId:this.executeId,
                  followName:this.strategyName,
                  pairMap:this.pairMap,
                  configMap:this.configMap,
                  followMap:this.followMap};
      navigator.open('','strategy/LaunchFollw', parm);
      broadcast.subscribeOnce('LaunchFollw', ()=>{
        this.isOpenFollow = true;
      });
    },

    //关闭跟单
    closeFollow(){ 

      var parm = {title:T("关闭跟单"),
                  message:T("关闭跟单后，跟单列表中将不可见此策略，原有跟随者不受影响"),
                  okBtn:T("确认"), cancelBtn:T("取消")};
      modal.confirm(parm, (bool)=>{
        if (bool == "true") {
          this.httCploseFollowStrategy();
        } else {
          this.isOpenFollow = true;
        }
      });
    }, 

    /********************************** 组件触发-UpFollow*/
    //跟单开关
    followChange(isOpenFollow){
      if (isOpenFollow) {
        this.openFollow();
      } else {
        this.closeFollow();
      }
    },

    /********************************** 点击触发*/
    //修改参数
    okClick() {

      //信号设置
      if (this.$refs.signal.checkError()) {
        return;
      }

      //高级设置
      if (this.$refs.VParam.checkError()) {
        return;
      }
      
      //仓位风控
      if (this.$refs.risk.checkError()) {
        return;
      }

      //其他设置
      if (this.$refs.upSet.checkError()) {
        return;
      }

      var signal =  this.$refs.signal.getParam(); 
      this.configMap.signal = signal;

      var reserves =  this.$refs.VParam.getParam(); 
      this.configMap.reserves = reserves;

      //仓位风控
      var riskParam = this.$refs.risk.getParam();
      this.configMap.tradePairs[0].openThreshold = riskParam.openThreshold;
      
      //其他设置
      var otherParam = this.$refs.upSet.getParam();
      this.configMap.stopProfit = otherParam.stopProfit;
      this.configMap.stopLoss = otherParam.stopLoss;
      this.configMap.stopTime = otherParam.stopTime;

      this.httpUpdateParam();
    },

    //参数值改变-回调
    paramChange(){
      this.upInvestLimit();
    },

    //更新投入限制
    upInvestLimit() {
      
      //保留位数
      var xiaoshu = this.getXiaoshu();
      
      //默认投入范围
      var minInvest = '100';
      var maxInvest = '5000';
    
      this.minInvest = minInvest;
      this.maxInvest = maxInvest;
    },
    
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.scroller {
  top: 0;
  right: 0;
  left: 0;
  z-index: 9;
}
.wrapper {
  width: 750px;
}
.line{
  width: 710px;
  height: 1px;
  margin-top: 40px;
  margin-bottom: 40px;
}
.control {
  margin-top: 40px;
  margin-left: 40px;
}

.senior{
  width: 670px;
  margin-left: 40px;
  margin-top: 60px;
}

.set{
  margin-top: 40px;
  margin-left: 40px;
}

.follow{
  width: 670px;
  margin-left: 40px;
  margin-top: 40px;
}


/*************** 启动*/
.ok{
  flex-direction: row;
  justify-content: center;
  width: 750px;
  height: 164px;
}
.ok-but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 670px;
  height: 80px;
  border-radius: 4px;
  margin-top: 28px;
}
.height80{
  height: 80px;
}
</style>